<%@page import="com.yunzhong.appointment.entity.Departmenttype"%>
<%@page import="com.yunzhong.appointment.entity.Department"%>
<%@page import="com.github.pagehelper.PageInfo"%>
<%@page import="com.yunzhong.appointment.entity.SysUser"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../../common/common.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>云众科技网上预约挂号系统</title>
	</head>
	<body>
		<ul id="myTab" class="nav nav-tabs">
			<li ><a href="#department" data-toggle="tab">科室信息</a></li>
		   	<li ><a href="#departmenttype" data-toggle="tab">科室类型信息</a></li>
		</ul>
		<div id="myTabContent" class="tab-content">
		   <div class="tab-pane fade in active" id="department">
		      <div class="main-container" id="main-container">
				<!-- /section:basics/sidebar -->
				<div class="main-content">
					<div class="main-content-inner">
						<div class="page-content">
							<div class="row">
								<div class="col-xs-12">
								<!-- 检索  -->
								<form  method="post" name="departmentForm" action="/sys/listDepartment">
									<input name="lastPage1" type="hidden">
									<input name="lastPage2" value="${pd.lastPage2}" type="hidden">
									<input name="dplName" value="${pd.dplName}" type="hidden">
									<div class="container">
										<div class="col-sm-4" style="margin-top: 20px;">
											<div class="input-group">
												<input type="text" class="form-control" name="dpName" placeholder="请输入科室名称" value="${pd.dpName }">
										      	<div class="input-group-btn">
						        					<button type="button" class="btn btn-warning" onclick="doSearch()"><span class="glyphicon glyphicon-search"></span></button>
										    	</div>
										    </div>
								    	</div>
								    	<div class="col-sm-6"></div>
								    	<div class="col-sm-2" style="margin-top: 20px;">
											<div class="form-group">
												<button type="button" class="btn btn-primary" title="增加" onclick="addpage()"><span class="glyphicon glyphicon-plus"></span></button>
												<button type="button" class="btn btn-danger" title="批量删除" onclick="del()"><span class="glyphicon glyphicon-trash"></span></button>
										    </div>
								    	</div>
								    </div>
									<table id="simple-table" class="table table-striped table-hover" style="margin-top:5px;">	
										<thead>
											<tr class="bg-primary">
												<td align="center"><a href="javascript:selectAll()"><font style="color: white;">全选</font></a></td>
												<td align="center">科室</td>
												<td align="center">科室类型</td>
												<td align="center">操作</td>
											</tr>
										</thead>
										<tbody>
										<!-- 开始循环 -->
										<%
											Department firstObj = (Department)request.getAttribute("firstObj");
											if(firstObj!=null){
												String firstId = firstObj.getDpId();
												//对page.list进行调整
												PageInfo p = (PageInfo)request.getAttribute("page");
												List<Department> list = p.getList();
												boolean flag = false;
												for(int i = 0; i < list.size(); i++){
													Department temDepartment = list.get(i);
													if(firstId.equals(temDepartment.getDpId())){
														//如果增加或修改的数据本身就在第一页，调整增加的数据到第一位
														Department department0 = list.get(0);
														list.set(0, temDepartment);
														list.set(i, department0);
														flag = true;
													}
												}
												if(!flag){
													//如果增加或修改的数据不再第一页，增加或修改的数据放在第一位
													list.add(0, firstObj);
													list.remove(list.size()-1);
												}
											}
										%>
										<c:choose>
											<c:when test="${not empty page.list}">
												<c:forEach items="${page.list}" var="department" varStatus="i">
													<tr>
														<td align="center"><input type="checkbox" name="ids" value="${department.dpId}"></td>
														<td align="center">${department.dpName}</td>
														<td align="center">${department.dplName}</td>
														<td  align="center">
															<button type="button" class="btn btn-primary" title="修改" onclick="editpage('${department.dpId}')"><span class="glyphicon glyphicon-edit"></span></button>
															<button type="button" class="btn btn-danger" title="删除" onclick="delOne('${department.dpId}')"><span class="glyphicon glyphicon-minus"></span></button>
															<button type="button" class="btn btn-warning" title="授权" onclick="auth('${department.dpId}')"><span class="glyphicon glyphicon glyphicon-link"></span></button>
														</td>
													</tr>
												</c:forEach>
											</c:when>
											<c:otherwise>
												<tr class="main_info">
													<td colspan="100" class="center" >没有相关数据</td>
												</tr>
											</c:otherwise>
										</c:choose>
										</tbody>
									</table>
									<c:if test="${page!=null }">
										<div align="center">
											<yzpage:page pageName="page" url="/sys/listDepartment"></yzpage:page>
										</div>
									</c:if>
								</form>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		   </div>
		   <div class="tab-pane fade" id="departmenttype">
		      <div class="main-container" id="main-container">
				<!-- /section:basics/sidebar -->
				<div class="main-content">
					<div class="main-content-inner">
						<div class="page-content">
							<div class="row">
								<div class="col-xs-12">
								<!-- 检索  -->
								<form  method="post" name="departmentTypeForm" action="/sys/listDepartmentType">
									<input name="lastPage1" value="${pd.lastPage1 }" type="hidden">
									<input name="lastPage2"  type="hidden">
									<input name="dpName" value="${pd.dpName }" type="hidden">
									<div class="container">
										<div class="col-sm-4" style="margin-top: 20px;">
											<div class="input-group">
												<input type="text" class="form-control" name="dplName" placeholder="请输入科室类型名称" value="${pd.dplName }">
										      	<div class="input-group-btn">
						        					<button type="button" class="btn btn-warning" onclick="doSearch()"><span class="glyphicon glyphicon-search"></span></button>
										    	</div>
										    </div>
								    	</div>
								    	<div class="col-sm-6"></div>
								    	<div class="col-sm-2" style="margin-top: 20px;">
											<div class="form-group">
												<button type="button" class="btn btn-primary" title="增加" onclick="addpage()"><span class="glyphicon glyphicon-plus"></span></button>
												<button type="button" class="btn btn-danger" title="批量删除" onclick="del()"><span class="glyphicon glyphicon-trash"></span></button>
										    </div>
								    	</div>
								    </div>
									<table id="simple-table" class="table table-striped table-hover" style="margin-top:5px;">	
										<thead>
											<tr class="bg-primary">
												<td align="center"><a href="javascript:selectAll()"><font style="color: white;">全选</font></a></td>
												<td align="center">科室类型</td>
												<td align="center">操作</td>
											</tr>
										</thead>
																
										<tbody>
										<!-- 开始循环 -->
										<%
											Departmenttype firstObj1 = (Departmenttype)request.getAttribute("firstObj1");
											if(firstObj1!=null){
												String firstId1 = firstObj1.getDplId();
												//对page.list进行调整
												PageInfo p1 = (PageInfo)request.getAttribute("page1");
												List<Departmenttype> list1 = p1.getList();
												boolean flag1 = false;
												for(int i = 0; i < list1.size(); i++){
													Departmenttype temDepartmenttype = list1.get(i);
													if(firstId1.equals(temDepartmenttype.getDplId())){
														//如果增加或修改的数据本身就在第一页，调整增加的数据到第一位
														Departmenttype illness1 = list1.get(0);
														list1.set(0, temDepartmenttype);
														list1.set(i, illness1);
														flag1 = true;
													}
												}
												if(!flag1){
													//如果增加或修改的数据不再第一页，增加或修改的数据放在第一位
													list1.add(0, firstObj1);
													list1.remove(list1.size()-1);
												}
											}
										%>
										<c:choose>
											<c:when test="${not empty page1.list}">
												<c:forEach items="${page1.list}" var="departmentType" varStatus="i">
													<tr>
														<td align="center"><input type="checkbox" name="ids" value="${departmentType.dplId}"></td>
														<td align="center">${departmentType.dplName}</td>
														<td  align="center">
															<button type="button" class="btn btn-primary" title="修改" onclick="editpage('${departmentType.dplId}')"><span class="glyphicon glyphicon-edit"></span></button>
															<button type="button" class="btn btn-danger" title="删除" onclick="delOne('${departmentType.dplId}')"><span class="glyphicon glyphicon-minus"></span></button>
															<button type="button" class="btn btn-warning" title="授权" onclick="auth('${departmentType.dplId}')"><span class="glyphicon glyphicon glyphicon-link"></span></button>
														</td>
													</tr>
												</c:forEach>
											</c:when>
											<c:otherwise>
												<tr class="main_info">
													<td colspan="100" class="center" >没有相关数据</td>
												</tr>
											</c:otherwise>
										</c:choose>
										</tbody>
									</table>
									<c:if test="${page1!=null }">
										<div align="center">
											<yzpage:page pageName="page1" url="/sys/listDepartmentType"></yzpage:page>
										</div>
									</c:if>
								</form>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		   </div>
		  
		</div>
		
	</body>
	<script type="text/javascript">
	
	$(function(){
		  //判断加载疾病还是疾病类型
		  var lis = $("#myTab").children();
		  var departmentDiv = $("#department");
		  var departmentTypeDiv = $("#departmenttype");
		  if("${page!=null}"=="true"){
			  $(lis[0]).attr("class","active");
			  $(lis[1]).attr("class","");
			  departmentDiv.attr("class","tab-pane fade in active");
			  departmentTypeDiv.attr("class","tab-pane fade");
		  }else{
			  $(lis[1]).attr("class","active");
			  $(lis[0]).attr("class","");
			  departmentDiv.attr("class","tab-pane fade");
			  departmentTypeDiv.attr("class","tab-pane fade in active");
		  } 
	      $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
	      var lastPage1 = $("input[name='lastPage1']");
	      var lastPage2 = $("input[name='lastPage2']");
	      var dpNames = $("input[name='dpName']");
	      var dplNames = $("input[name='dplName']");
	      var activeTab = $(e.target).text(); 
	      if(activeTab == "科室类型信息"){
	    	  lastPage1[1].value = "${page.pageNum}";
	    	  lastPage2[1].value = "${pd.lastPage2}";
	    	  dpNames[1].value = dpNames[0].value;
	    	  dplNames[1].value = "${pd.dplName}"
	    	  $("form[name='departmentTypeForm']").submit();
	      }else{
	    	  lastPage1[0].value = "${pd.lastPage1}";
	    	  lastPage2[0].value = "${page1.pageNum}";
	    	  dplNames[0].value = dplNames[1].value;
	    	  dpNames[0].value = "${pd.dpName}";
	    	  $("form[name='departmentForm']").submit();
	      }
	   });
	});
	
	function editpage(id){
		if("${page!=null}"=="true"){
			location.href = "/sys/pageDepartment?id="+id;
		}else{
			location.href = "/sys/pageDepartmentType?id="+id;
		}
	}
	function addpage(){
		if("${page!=null}"=="true"){
			location.href = "/sys/pageDepartment";
		}else{
			location.href = "/sys/pageDepartmentType";
		}
	}
	
	function delOne(id){
		bootbox.confirm("确定要删除吗?", function(result) {
			if(result){
				if("${page!=null}"=="true"){
					location.href = "/sys/removeDepartment?ids="+id;
				}else{
					location.href = "/sys/removeDepartmentType?ids="+id;
				}
			}
		})
	}
	
	function doSearch(){
		if("${page!=null}"=="true"){
			$("form[name='departmentForm']").attr("action","/sys/listDepartment");
			$("form[name='departmentForm']").submit();
		}else{
			$("form[name='departmentTypeForm']").attr("action","/sys/listDepartmentType");
			$("form[name='departmentTypeForm']").submit();
		}
	}

	//全选
	function selectAll(){
		var ipts;
		if("${page!=null}"=="true"){
			inpts = $("form[name='departmentForm'] input[name='ids']");
		}else{
			inpts = $("form[name='departmentTypeForm'] input[name='ids']");
		}
		var inpts = $("input[name='ids']");
		for(var i=0;i<inpts.length;i++){
			if(inpts[i].checked!=true){
				inpts[i].checked=true;
			}else{
				inpts[i].checked=false;
			}
		}
	}
	//批量操作
	function del(){
		bootbox.confirm("确定要删除选中的数据吗?", function(result) {
			if(result) {
				var formName;
				var url;
				if("${page!=null}"=="true"){
					formName = "departmentForm";
					url = "/sys/removeDepartment"
				}else{
					formName = "departmentTypeForm";
					url = "/sys/removeDepartmentType"
				}
				var checked = $("form[name='"+formName+"'] :input:checked");
				if(checked.length==0){
					bootbox.dialog({
						message: "<span class='bigger-110'>您没有选择任何内容!</span>",
						buttons: 			
						{ "button":{ "label":"确定", "className":"btn-sm btn-success"}}
					});
					$("#zcheckbox").tips({
						side:1,
			            msg:'点这里全选',
			            bg:'#AE81FF',
			            time:8
			        });
					return;
				}else{
					$("form[name='"+formName+"']").attr("action",url);
					$("form[name='"+formName+"']").submit();
				}
			}
		});
	};
	</script>
</html>